package com.titan.core.utils.dbinfo.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

import com.titan.core.utils.db.DBUtils;
import com.titan.core.utils.dbinfo.interfaces.TableInfo;


/**
 * @author 周成功
 * @email 86898837@qq.com
 * @date 2020年02月20日
 */
public class TableInfoSqlImpl implements TableInfo {
    // {列英文名, 数据类型, 数据长度, 数值指定长度(整数+小数), 数值小数部分长度, 是否可以为null(Y/N), 列注释}
    public String[][] getColumnInfo(Connection conn, String tablename) throws Exception {
        return getColumnInfo(conn, null, tablename);
    }
    public String[][] getColumnInfo(Connection conn, String schema, String tablename) throws Exception {
        Statement stmt = null;
        ResultSet rest = null;
        try {
            stmt = conn.createStatement();
            rest = stmt.executeQuery(tablename);
            ResultSetMetaData rsm = rest.getMetaData();
            String[][] result = new String[rsm.getColumnCount()][7];
            for(int row=0; row<rsm.getColumnCount(); row++){   
                result[row][0] = rsm.getColumnName(row+1);
                result[row][1] = rsm.getColumnTypeName(row+1);
                result[row][2] = "0";
                result[row][3] = Integer.toString(rsm.getPrecision(row+1));
                result[row][4] = Integer.toString(rsm.getScale(row+1));
                result[row][5] = "Y";
                result[row][6] = "";
            } 
            return result;
        } finally {
            DBUtils.close(stmt);
            DBUtils.close(rest);
        }
    }
    
    
    public String[] getPrimaryKey(Connection conn, String tablename) throws Exception {
        return getPrimaryKey(conn, null, tablename);
    }
    public String[] getPrimaryKey(Connection conn, String schema, String tablename) throws Exception {
        return new String[0];
    }
    
    
    // 表类型, 表注释
    public String[] getTableInfo(Connection conn, String tablename) throws Exception {
        return getTableInfo(conn, null, tablename);
    }
    public String[] getTableInfo(Connection conn, String schema, String tablename) throws Exception {
        // 获得表信息
        return null;
    }
    @Override
    public String[][] getUniqueIndex(Connection conn, String tablename) throws Exception {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public String[][] getUniqueIndex(Connection conn, String schema, String tablename) throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

}
